Ext.onReady(function(){ 
var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'流水号',dataIndex:'serialNo',hidden:true},
{header:'外部评级机构',dataIndex:'extRatOrg',width:100},
{header:'外部评级结果',dataIndex:'extRatResult',width:100},
{header:'外部评级时间',dataIndex:'extRatDate',width:120},
]); 
var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listExtrat.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'serialNo'},
{name:'extRatOrg'},
{name:'extRatResult'}, 
{name:'extRatDate'} 
]) 
}); 
ds.load({params:{start:0,limit:10}}); 

var addExtRatOrgStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=ExtRatOrg',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
addExtRatOrgStore.load();

var grid=new Ext.grid.GridPanel({ 
id:'myCustomers',
renderTo:"customer_list", 
stripeRows:true,//斑马线效果
title:"外部评级列表", 
height:500, 
cm:cm,
store: ds,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:checkMyCustomer},
 '-',
 {text:'删除',handler:deleteCustomer}
 ]              
});

//新增客户表单
addForm = new Ext.form.FormPanel({ 
id: 'addCustomerForm', 
width: 400, 
height: 220, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth:100, 
items: 
[ { xtype: 'fieldset', 
title: '评级信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[

{  columnWidth : .5, 	      	   
	 layout : "form",
           items : [{
xtype: 'combo', 
 id : "extRatOrg",
              store :addExtRatOrgStore,
              fieldLabel: '外部评级机构',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择外部评级机构...',
              editable: false,
              typeAhead : true,
              width: 150,
              mode:'remote',  
              hiddenName:'extRatOrg',
              triggerAction : 'all',
              selectOnFocus : true
}]} ,

{ layout : "form",
           items : [{
xtype: 'textfield',
id: 'extRatResult', 
name: 'extRatResult', 
fieldLabel: '外部评级结果',  
allowBlank: true,  
width:150
}]},

{ 
layout : "form",
	items : [{
xtype: 'datefield',
id: 'extRatDate', 
name: 'extRatDate', 
fieldLabel: '外部评级时间',  
allowBlank: true,  
width:150,
format : 'Y-m-d'
}]} ,

{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "serialNo",
              fieldLabel: '评级ID',
              allowBlank: true
}]}
]}]});

//新增用户窗口 
addWin = new Ext.Window({ 
id: 'addCustomerWin', 
title: '新增客户-外部评级信息', 
width: 400, 
height: 250, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addCustomerWin').hide(); } }

] 
});

function addRoleFunction() {

	
	var addForm = Ext.getCmp("addCustomerForm");

if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 

                    url : 'customer/addExtrat.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('myCustomers').store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存未成功！'); 
 
                        } 
                    });  
                    }
 }
  


//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp("addCustomerWin").show();
addForm.getForm().getEl().dom.reset(); 
Ext.getCmp("addCustomerWin").hide(); 
Ext.getCmp("addCustomerWin").buttons[0].handler = addRoleFunction; 
Ext.getCmp("addCustomerWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除地址
function deleteCustomer(){
var grid = Ext.getCmp('myCustomers'); 
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要删除的项！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选项吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
	//alert(grid.getSelectionModel().getSelections()[0].data.serialNo);
 Ext.Ajax.request({
  url:'customer/deleteExtrat.do',
  params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   Ext.getCmp('myCustomers').store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

function checkMyCustomer(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要查看的客户！"); 
}
else{
		var serialNo = grid.getSelectionModel().getSelections()[0].data.serialNo;
	
	Ext.getCmp('addCustomerForm').form.reset(); 
	Ext.getCmp("addCustomerWin").buttons[0].handler = editAddressFunction; 
	
           Ext.Ajax.request({
url:'customer/getExtrat.do',
params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
method:'POST',
   success:function(response){

 var addJSON = Ext.util.JSON.decode(response.responseText);
 
 	Ext.getCmp("addCustomerWin").show();
  	Ext.getCmp("serialNo").setValue(addJSON.serialNo);  
	Ext.getCmp("extRatOrg").setValue(addJSON.extRatOrg);
  	Ext.getCmp("extRatResult").setValue(addJSON.extRatResult);
  	Ext.getCmp("extRatDate").setValue(addJSON.extRatDate);  	 
  		
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}


//编辑地址
function editAddressFunction(){
	
	var addForm = Ext.getCmp("addCustomerForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 

                    url : 'customer/editExtrat.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('myCustomers').store.reload();
                       Ext.Msg.alert('提示', '编辑成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '编辑失败！'); 
 
                        } 
                    }); 
                    }
}

function rowdblclickFn(grid, rowIndex, e){//双击事件  
             var row = grid.getStore().getAt(rowIndex); 
             var selectedCustId = row.get("custId");
             Ext.Ajax.request({
  url:'checkRequest.do',
  method:'GET',
  success:function(){
  window.location.href="checkMyCustomer.do?CustId="+selectedCustId;
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
});  
           //  window.location.href="view/customer/CheckCustomer_General.jsp?custId="+selectedCustId;
        }   
        grid.addListener('rowdblclick',checkMyCustomer);
/*grid.on('rowdblclick',function(){ window.location.href="view/customer/CheckCustomer_General.jsp" });*/
}); 